package cn.kgc.shiro02.controller;

import cn.kgc.shiro02.commons.CodeMsg;
import cn.kgc.shiro02.commons.ResponseResult;
import cn.kgc.shiro02.entity.Permission;
import cn.kgc.shiro02.service.PermissionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/4/7
 * @Description: 权限控制层
 */
@Api(tags = "权限模块")
@RequestMapping("permission")
@RestController
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @GetMapping("list")
    public ResponseResult  getList(){
        List<Permission> list = permissionService.list();
        return  new ResponseResult(CodeMsg.SUCCESS,null,list);
    }

    @GetMapping("treeSelect")
    public ResponseResult  treeSelect(){

        LambdaQueryWrapper<Permission> lambda = new QueryWrapper<Permission>().lambda();
        lambda.in(Permission::getIsMenu, Arrays.asList(0,1));
        List<Permission> list = permissionService.list(lambda);

        return   new ResponseResult(CodeMsg.SUCCESS,null,list);

    }


    @PostMapping("add")
    public ResponseResult  addPermission(Permission permission){
        permissionService.save(permission);
        return   new ResponseResult(CodeMsg.SUCCESS);
    }

    @PostMapping("update")
    public ResponseResult  updatePermission(Permission permission){
        permissionService.updateById(permission);
        return   new ResponseResult(CodeMsg.SUCCESS);
    }


    @GetMapping("selectById")
    public ResponseResult selectById(Integer id){

        Permission permission = permissionService.getById(id);

         return new ResponseResult(CodeMsg.SUCCESS,null,permission);

    }

    @GetMapping("delete")
    public ResponseResult deleteById(Integer id){

        return  permissionService.deleteById(id);

    }


}
